//
//  BoZeAddOilVC.swift
//  BoZeCalendar
//
//  Created by 李东 on 2024/4/12.
//

import UIKit

class BoZeAddOilVC: BoZeBaseVC {
    
    @IBOutlet weak var ymdLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    
    @IBOutlet weak var kmTF: UITextField!
    @IBOutlet weak var addOilTF: UITextField!
    @IBOutlet weak var priceTF: UITextField!
    @IBOutlet weak var costLabel: UILabel!
    @IBOutlet weak var remianTF: UITextField!
    @IBOutlet weak var summitBtn_top: NSLayoutConstraint!
    var model = BoZeCarModel()
    
    init(model: BoZeCarModel = BoZeCarModel()) {
        super.init(nibName: nil, bundle: nil)
        self.model = model
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        hideNavgaitionBar()
        showBgImgView = true
        bgImgView.image = UIImage(named: "img_me_bg")
        showPopBtn = true
        navTitle = "记油耗"
        summitBtn_top.constant = CGFloat.topPadding
        

        priceTF.addTarget(self, action: #selector(inputDidChanged(sender:)), for: .editingChanged)
        addOilTF.addTarget(self, action: #selector(inputDidChanged(sender:)), for: .editingChanged)
        
        ymdLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showDatePicker)))
        timeLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showDatePicker)))
        
        let list = df2.string(from: Date()).components(separatedBy: " ")
        ymdLabel.text = list[0]
        timeLabel.text = list[1]
    }

    lazy var df: DateFormatter = {
        let temp = DateFormatter()
        temp.dateFormat = "yyyy年MM月dd日"
        return temp
    }()
    
    lazy var df1: DateFormatter = {
        let temp = DateFormatter()
        temp.dateFormat = "HH:mm"
        return temp
    }()
    
    lazy var df2: DateFormatter = {
        let temp = DateFormatter()
        temp.dateFormat = "yyyy年MM月dd日 HH:mm"
        return temp
    }()
    
    @objc func showDatePicker() {
        let vc = BoZeDatePickerVC()
        vc.selectCallBack = {[weak self] date in
            guard let self = self else { return  }
            let list = self.df2.string(from: date).components(separatedBy: " ")
            self.ymdLabel.text = list[0]
            self.timeLabel.text = list[1]
        }
        vc.modalPresentationStyle = .overFullScreen
        present(vc, animated: true)
    }

    
    @IBAction func summitHandle(_ sender: Any) {
        if kmTF.text?.isEmpty ?? true {
            showHUDTextOnly("请输入当前里程数")
            return
        }
        if priceTF.text?.isEmpty ?? true {
            showHUDTextOnly("请输入油价")
            return
        }
        if addOilTF.text?.isEmpty ?? true {
            showHUDTextOnly("请输入加油量")
            return
        }
        if remianTF.text?.isEmpty ?? true {
            showHUDTextOnly("请输入未加油时油量")
            return
        }
        let km = Int(kmTF.text!) ?? 0
        if !model.list.isEmpty && model.list[0].km > km {
            showHUDTextOnly("本次里程数不能小于上次里程数")
            return
        }
        let remainOil = Int(remianTF.text!) ?? 0
        if !model.list.isEmpty && model.list[0].addOil < remainOil {
            showHUDTextOnly("剩余油量不能大于上次加油量")
            return
        }

        
        let str = ymdLabel.text! + " " + timeLabel.text!
        let tempDate = df2.date(from: str)!
        let price = String(format: "%.2f", CGFloat(Double(priceTF.text ?? "0") ?? 0 ))
        let cost = costLabel.text ?? "0"
        let addOil = Int(addOilTF.text!) ?? 0
        let tempModel = BoZeOilModel(ts: Int(tempDate.timeIntervalSince1970), ymd_time: str, price: price, km: km, addOil: addOil, remainOil: remainOil,cost: cost)
        self.model.addOil(mm: tempModel)
        showHUDTextOnly("已保存") {[weak self] in
            guard let self = self else {
                return
            }
            self.navigationController?.popViewController(animated: true)
        }
    }
    

    @objc func inputDidChanged(sender: UITextField) {
        let price = CGFloat(Double(priceTF.text ?? "0") ?? 0 )
        let oil = CGFloat(Double(addOilTF.text ?? "0") ?? 0 )
        costLabel.text = String(format: "%.2f", price * oil)
    }
}
